package top.zlhy7.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 任勇
 * @date 2022/8/2 20:20
 * @description 线程之间的通信
 * AA打印5次，BB打印10次，CC打印15次
 * 再来一遍
 */
//第一步 创建资源类,定义属性方法
class ShareResource{
    private int flag = 1;//1 AA,2 BB,3 CC
    /**
     * 锁对象
     */
    private Lock lock = new ReentrantLock();
    /**
     * 创建三个条件
     */
    private Condition c1 = lock.newCondition();
    /**
     * 创建三个条件
     */
    private Condition c2 = lock.newCondition();
    /**
     * 创建三个条件
     */
    private Condition c3 = lock.newCondition();

    /**
     * 打印五次，打印第几次了
     * @param loop
     */
    public void print5(int loop){
        lock.lock();
        try {
            //判断
            while (flag!=1){
                //等待
                c1.await();
            }
            for (int i = 0; i < 5; i++) {
                //打印
                System.out.printf("%s i:%d,loop:%d\n",Thread.currentThread().getName(),i,loop);
            }
            //修改标识位，通知BB
            flag = 2;
            c2.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    /**
     * 打印10次，打印第几次了
     * @param loop
     */
    public void print10(int loop){
        lock.lock();
        try {
            //判断
            while (flag!=2){
                //等待
                c1.await();
            }
            for (int i = 0; i < 10; i++) {
                //打印
                System.out.printf("%s i:%d,loop:%d\n",Thread.currentThread().getName(),i,loop);
            }
            //修改标识位，通知BB
            flag = 3;
            c3.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    /**
     * 打印15次，打印第几次了
     * @param loop
     */
    public void print15(int loop){
        lock.lock();
        try {
            //判断
            while (flag!=3){
                //等待
                c1.await();
            }
            for (int i = 0; i < 15; i++) {
                //打印
                System.out.printf("%s i:%d,loop:%d\n",Thread.currentThread().getName(),i,loop);
            }
            //修改标识位，通知BB
            flag = 1;
            c1.signal();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
public class ThreadDemo3 {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                shareResource.print5(i);
            }
        },"AA").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                shareResource.print10(i);
            }
        },"BB").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                shareResource.print15(i);
            }
        },"CC").start();
    }
}
